<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>研学选择数据管理 - 选择数据列表</title>
    <link href="<?= base_url('css/font-awesome.min.css') ?>" rel="stylesheet">
    <script src="<?= base_url('js/tailwind.min.js') ?>"></script>

    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#1E40AF',
                        accent: '#3B82F6',
                        neutral: '#F3F4F6',
                        'neutral-dark': '#1F2937',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>

    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }

            .card-shadow {
                box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            }

            .hover-scale {
                transition: transform 0.3s ease;
            }

            .hover-scale:hover {
                transform: scale(1.02);
            }
        }
    </style>
</head>

<body class="bg-gray-50 font-sans">
    <!-- 顶部导航 -->
    <header class="bg-white shadow-md fixed top-0 left-0 right-0 z-50 transition-all duration-300">
        <div class="container mx-auto px-4 py-3 flex items-center justify-between">
            <div class="flex items-center space-x-2">
                <i class="fa-solid fa-graduation-cap text-primary text-2xl"></i>
                <h1 class="text-xl font-bold text-gray-800">研学基地信息系统</h1>
            </div>
            <button class="md:hidden text-gray-700">
                <i class="fa-solid fa-bars text-xl"></i>
            </button>
        </div>
    </header>

    <!-- 主内容区 -->
    <div class="pt-16 pb-8 min-h-screen">
        <div class="container mx-auto px-4">
            <!-- 页面标题 -->
            <div class="mt-8 mb-6 flex flex-col md:flex-row md:items-center md:justify-between">
                <div>
                    <h2 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-gray-800">选择数据列表</h2>
                    <p class="text-gray-600 mt-1">查看和管理学生的研学地点选择</p>
                </div>
                <div class="mt-4 md:mt-0 flex space-x-3">
                    <div class="relative">
                        <input type="text" placeholder="搜索学生姓名" class="pl-10 pr-4 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all duration-200 w-full md:w-64">
                        <i class="fa-solid fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                    </div>
                    <button id="exportDataBtn" class="bg-primary hover:bg-primary/90 text-white font-medium py-2 px-4 rounded-lg transition-all duration-200 flex items-center">
                        <i class="fa-solid fa-download mr-2"></i>导出数据
                    </button>
                </div>
            </div>

            <!-- 数据表格 -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6 border-b border-gray-100">
                    <div class="flex flex-col md:flex-row md:items-center md:justify-between">
                        <h3 class="font-bold text-gray-800 text-lg">选择数据列表</h3>
                        <div class="mt-4 md:mt-0 flex space-x-3">
                            <div class="relative">
                                <select id="classSelect" class="pl-4 pr-10 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all duration-200 appearance-none bg-white">
                                    <option>所有班级</option>
                                    <option>五（1）班</option>
                                    <option>五（2）班</option>
                                </select>
                                <i class="fa-solid fa-chevron-down absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 pointer-events-none"></i>
                            </div>
                            <button class="bg-primary hover:bg-primary/90 text-white font-medium py-2 px-4 rounded-lg transition-all duration-200 flex items-center">
                                <i class="fa-solid fa-refresh mr-2"></i>刷新
                            </button>
                        </div>
                    </div>
                </div>

                <div class="overflow-x-auto">
                    <table id="dataTable" class="w-full">
                        <thead>
                            <tr class="bg-gray-50">
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">序号</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">班级</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">姓名</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">第一选择</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">第二选择</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">第三选择</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php 
                            $totalRecords = count($selections);
                            $recordsPerPage = 50;
                            $totalPages = ceil($totalRecords / $recordsPerPage);
                            $currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1;
                            $startIndex = ($currentPage - 1) * $recordsPerPage;
                            $endIndex = min($startIndex + $recordsPerPage, $totalRecords);
                            for ($i = $startIndex; $i < $endIndex; $i++) {
                                $selection = $selections[$i];
                            ?>
                            <tr class="hover:bg-gray-50 transition-colors duration-150">
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?= $i + 1 ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <div class="flex items-center">
                                        <div class="w-3 h-3 rounded-full bg-<?= $selection['class_name'] === '五（1）班' ? 'blue' : 'purple' ?>-500 mr-2"></div>
                                        <?= $selection['class_name'] ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900"><?= $selection['student_name'] ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
                                        <?= $selection['first_choice'] ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                        <?= $selection['second_choice'] ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-purple-100 text-purple-800">
                                        <?= $selection['third_choice'] ?>
                                    </span>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>

                <div class="px-6 py-4 bg-gray-50 border-t border-gray-200 flex items-center justify-between">
                    <div class="text-sm text-gray-700">
                        显示 <span class="font-medium"><?= $startIndex + 1 ?></span> 到 <span class="font-medium"><?= $endIndex ?></span> 条，共 <span class="font-medium"><?= $totalRecords ?></span> 条记录
                    </div>
                    <div class="flex space-x-2">
                        <button id="prevPageBtn" class="px-3 py-1 border border-gray-300 rounded-md text-gray-700 bg-white hover:bg-gray-50 <?php if ($currentPage == 1) echo 'disabled:opacity-50 disabled:cursor-not-allowed' ?>" <?php if ($currentPage == 1) echo 'disabled' ?>>
                            <i class="fa-solid fa-chevron-left"></i>
                        </button>
                        <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
                        <button class="px-3 py-1 border border-gray-300 rounded-md text-gray-700 bg-white hover:bg-gray-50 <?php if ($i == $currentPage) echo 'bg-primary text-white' ?>" onclick="window.location.href='?page=<?= $i ?>'"><?= $i ?></button>
                        <?php } ?>
                        <button id="nextPageBtn" class="px-3 py-1 border border-gray-300 rounded-md text-gray-700 bg-white hover:bg-gray-50 <?php if ($currentPage == $totalPages) echo 'disabled:opacity-50 disabled:cursor-not-allowed' ?>" <?php if ($currentPage == $totalPages) echo 'disabled' ?>>
                            <i class="fa-solid fa-chevron-right"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="bg-white border-t border-gray-200 py-6">
        <div class="container mx-auto px-4">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between">
                <div class="mb-4 md:mb-0">
                    <p class="text-gray-600 text-sm">&copy; 2025 研学基地信息系统. 保留所有权利.</p>
                </div>
                <div class="flex space-x-6">
                    <a href="#" class="text-gray-600 hover:text-primary transition-colors">
                        <i class="fa-solid fa-file-text"></i> 文档
                    </a>
                    <a href="#" class="text-gray-600 hover:text-primary transition-colors">
                        <i class="fa-solid fa-question-circle"></i> 帮助中心
                    </a>
                    <a href="#" class="text-gray-600 hover:text-primary transition-colors">
                        <i class="fa-solid fa-envelope"></i> 联系我们
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 导出数据功能
            const exportDataBtn = document.getElementById('exportDataBtn');
            exportDataBtn.addEventListener('click', function() {
                const table = document.getElementById('dataTable');
                const rows = table.rows;
                let csvContent = "\uFEFF"; // 添加 UTF-8 BOM
                for (let i = 0; i < rows.length; i++) {
                    const row = rows[i];
                    const cols = row.cells;
                    let rowData = [];
                    for (let j = 0; j < cols.length; j++) {
                        let cellValue = cols[j].textContent.trim();
                        // 处理特殊字符
                        if (cellValue.includes(',') || cellValue.includes('"')) {
                            cellValue = '"' + cellValue.replace(/"/g, '""') + '"';
                        }
                        rowData.push(cellValue);
                    }
                    csvContent += rowData.join(",") + "\n";
                }
                const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
                const url = URL.createObjectURL(blob);
                const link = document.createElement("a");
                link.setAttribute("href", url);
                link.setAttribute("download", "selection_data.csv");
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
                URL.revokeObjectURL(url);
            });

            // 上一页按钮
            const prevPageBtn = document.getElementById('prevPageBtn');
            prevPageBtn.addEventListener('click', function() {
                const currentPage = parseInt(new URLSearchParams(window.location.search).get('page') || 1);
                if (currentPage > 1) {
                    window.location.href = `?page=${currentPage - 1}`;
                }
            });

            // 下一页按钮
            const nextPageBtn = document.getElementById('nextPageBtn');
            nextPageBtn.addEventListener('click', function() {
                const currentPage = parseInt(new URLSearchParams(window.location.search).get('page') || 1);
                const totalPages = <?php echo $totalPages; ?>;
                if (currentPage < totalPages) {
                    window.location.href = `?page=${currentPage + 1}`;
                }
            });

            // 班级筛选功能
            const classSelect = document.getElementById('classSelect');
            const tableRows = document.querySelectorAll('#dataTable tbody tr');

            classSelect.addEventListener('change', function() {
                const selectedClass = this.value;
                tableRows.forEach(row => {
                    const className = row.cells[1].textContent.trim();
                    if (selectedClass === '所有班级' || className === selectedClass) {
                        row.style.display = '';
                    } else {
                        row.style.display = 'none';
                    }
                });
            });
        });
    </script>
</body>

</html>